#include "BlockQueue.hpp"
#include "Task.hpp"
#include <ctime>
#include <unistd.h>

int myadd(int x, int y)
{
    return x + y;
}
void *consumer(void *bq_)
{
    BlockQueue<Task> *bq = static_cast<BlockQueue<Task> *>(bq_);
    while (1)
    {
        // 消费活动
        Task t;
        bq->pop(&t);
        std::cout << "消费数据：" << t() << std::endl;
        sleep(1);
    }
    return nullptr;
}

void *productor(void *bq_)
{
    BlockQueue<Task> *bq = static_cast<BlockQueue<Task> *>(bq_);

    while (1)
    {
        // 生产活动
        int x = rand() % 10 + 1;
        int y = rand() % 20 + 1;
        Task t(x, y, myadd);
        bq->push(t);
        // std::cout << "生产数据：" << data << std::endl;
        // sleep(1);
    }
    return nullptr;
}
int main()
{
    srand((unsigned long)time(nullptr) ^ getpid());
    BlockQueue<Task> *bq = new BlockQueue<Task>();
    pthread_t c, p;
    pthread_create(&c, nullptr, consumer, bq);
    pthread_create(&p, nullptr, productor, bq);

    pthread_join(c, nullptr);
    pthread_join(p, nullptr);

    delete bq;
    return 0;
}